//引入moment.js
import moment from 'moment';
/**
 * @description 获取一年中有多少周、每一周开始和结束时间 当前是哪一周
 * @param {Number} year 年份
 * @param {Boolean} isSurplus 是否只取剩余年份
 * @returns 
 * {
    "arryWeek": [
        {
            "value": 1,
            "name": "第1周 12-26 至 01-01",
            "startTime": "2021-12-26",
            "endTime": "2022-01-01"
        }
    ],
    "currentWeek": 24
}
 */
const mapWeeksOfyear = (year, isSurplus) => {
    const nowYear = year ? year : moment().year();
    const timestamp = (new Date()).valueOf()
    // 设置正在处理的年份
    let handleYear = moment(new Date(String(nowYear)));
    // 这一年有多少个周
    const totalWeeks = handleYear.endOf('year').isoWeek();
    const arryWeek = [];
    let currentWeek = null; //当前日期是第几周
    for (let i = 1; i <= totalWeeks; i++) {
        let startOf = handleYear.week(i).startOf('week').format('MM-DD');
        let endOf = handleYear.week(i).endOf('week').format('MM-DD');
        let ednyear = handleYear.week(i).endOf('week').format('YYYY');
        let startValue = handleYear.week(i).startOf('week').valueOf();
        let endValue = handleYear.week(i).endOf('week').valueOf();
        if (startValue <= timestamp && endValue >= timestamp) {
            currentWeek = i
        }
        let startTime = handleYear.week(i).startOf('week').format('YYYY-MM-DD')// 这周的开始时间
        let endTime = handleYear.week(i).endOf('week').format('YYYY-MM-DD') // 这周的结束时间
        function getArrWeek() {
            arryWeek.push({
                value: i,
                name: `第${i}周 ${ednyear > nowYear ? nowYear + "-" + startOf : startOf} 至 ${ednyear > nowYear ? ednyear + "-" + endOf : endOf}`,
                startTime: startTime,
                endTime: endTime
            })
        }
        // 当前时间和指定时间对比，如果当前时间小于指定时间就放入数组中，并且第一项不跨年
        if (isSurplus) {
            if (moment().diff(moment(new Date().getFullYear() + '-' + startOf), 'days') <= 0 && startTime.slice(0, 4) == endTime.slice(0, 4)) {
                getArrWeek()
            }

        } else {
            getArrWeek()
        }

    }
    return { arryWeek, currentWeek }
}
/**
 * @description 获取一年中有多少周、每一周开始和结束时间 每个月里面有哪些周 当前是哪一周
 * @param {Number} year 年份
 * @param {Boolean} isSurplus 是否只取剩余年份
 * @returns 
 * {
    "arryWeek": [
        {
            "value": 1,
            "name": "1月",
            data:'01-01 至 01-30',
            children: [ 
                {
                    "value": 1,
                    "name": "第一周",
                     data:'01-01 至 01-30',
                }
            ]
        }
    ],
    "currentWeek": 24,
    "currentMounth": 6
}
 */
const mapWeeksAndMonthOfyear = (year, isSurplus) => {
    const nowYear = year ? year : moment().year();
    const timestamp = (new Date()).valueOf()
    // 设置正在处理的年份
    let handleYear = moment(new Date(String(nowYear)));
    // 这一年有多少个周
    const totalWeeks = handleYear.endOf('year').isoWeek();
    const arryWeek = [];
    let currentWeek = null; //当前日期是第几周
    let monthArr = [1,2,3,4,5,6,7,8,9,10,11,12] //月份
    for (let i = 1; i <= totalWeeks; i++) {
        // 每周的开始时间
        let startOf = handleYear.week(i).startOf('week').format('MM-DD');
        // 每周周所在的月份
        let weekMonth = handleYear.week(i).startOf('week')._d.getMonth()+1
 
        let endOf = handleYear.week(i).endOf('week').format('MM-DD');
        let ednyear = handleYear.week(i).endOf('week').format('YYYY');
        let startValue = handleYear.week(i).startOf('week').valueOf();
        let endValue = handleYear.week(i).endOf('week').valueOf();
        if (startValue <= timestamp && endValue >= timestamp) {
            currentWeek = i
        }
        let startTime = handleYear.week(i).startOf('week').format('YYYY-MM-DD')// 这周的开始时间
        let endTime = handleYear.week(i).endOf('week').format('YYYY-MM-DD') // 这周的结束时间
        monthArr.forEach((item,index)=>{
            // 月份相同的周，要放入到对应的月数组中
            if(weekMonth == item){
                arryWeek[index] = {
                    "value": item,
                    "name": item+'月',
                    data:'01-01 至 01-30',
                }
            }
        })
        function getArrWeek() {
            arryWeek.push({
                value: i,
                name: `第${i}周 ${ednyear > nowYear ? nowYear + "-" + startOf : startOf} 至 ${ednyear > nowYear ? ednyear + "-" + endOf : endOf}`,
                startTime: startTime,
                endTime: endTime
            })
        }
        getArrWeek()

    }
    return { arryWeek, currentWeek }
}
/**
 * @description 计算x年后的今天
 * @param {Number} yearNum 多少年后
 */
const getNextYear = (yearNum) => {
    //注意事项：4年中必定有一年是润年，所以必定有一天是366天，其余3年均是365天
    //获取系统当前时间
    var curDate = (new Date()).getTime();
    // 将4年的时间单位换算成毫秒
    var fourYearsTime = (365 * yearNum + 366) * 24 * 60 * 60 * 1000;
    //用当前时间+4年时间，得到4年后当前时间
    var result = curDate + fourYearsTime;
    var resultDate = new Date(result);
    var year = resultDate.getFullYear();
    var month = resultDate.getMonth() + 1;
    var day = resultDate.getDate();
    return year + "-" + month + "-" + day
}
export default {
    getNextYear,
    mapWeeksOfyear,
    mapWeeksAndMonthOfyear
}